/**
 * 
 */
package com.swim.natatoriuminfo.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.common.web.CookieUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import com.swim.natatoriuminfo.entity.ClickStats;
import com.swim.natatoriuminfo.entity.NatatoriumInfo;
import com.swim.natatoriuminfo.entity.NatatoriumInfo1;
import com.swim.natatoriuminfo.service.NatatoriumInfoService;
import com.swim.natatoriuminfo.service.NatatoriumInfoService1;
import com.swim.natatoriuminfotouser.entity.NatatoriumInfoToUser;
import com.swim.natatoriuminfotouser.service.NatatoriumInfoToUserService;

/**
 * 游泳馆信息Controller
 * @author tql
 * @version 2021-03-14
 */
@Controller
@RequestMapping(value = "${adminPath}/natatoriuminfo/natatoriumInfo")
public class NatatoriumInfoController extends BaseController {

	@Autowired
	private NatatoriumInfoService natatoriumInfoService;
	
	@Autowired
	private NatatoriumInfoService1 natatoriumInfoService1;
	
	@Autowired
	private NatatoriumInfoToUserService natatoriumInfoToUserService;
	
	/**
	 * 跳转首页
	 */
	@RequestMapping(value = {"home", ""})
	public String home(NatatoriumInfo natatoriumInfo, Model model) {
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/home/home";
	}
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public NatatoriumInfo get(String id, boolean isNewRecord) {
		return natatoriumInfoService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(NatatoriumInfo natatoriumInfo, Model model) {
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumInfoList";
	}
	
	/**
	 * 查询列表(运营只能查看最近的3条)
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = {"listTop3", ""})
	public String listTop3(NatatoriumInfo natatoriumInfo, Model model) {
		model.addAttribute("natatoriumInfo1", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumInfoTop3List";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<NatatoriumInfo> listData(NatatoriumInfo natatoriumInfo, HttpServletRequest request, HttpServletResponse response) {
		Page<NatatoriumInfo> page = natatoriumInfoService.findPage(new Page<NatatoriumInfo>(request, response), natatoriumInfo); 
		return page;
	}
	
	/**
	 * 查询列表数据(运营只能查看最近的3条)
	 */
	@RequestMapping(value = "listTop3Data")
	@ResponseBody
	public Page<NatatoriumInfo1> listTop3Data(NatatoriumInfo1 natatoriumInfo1, HttpServletRequest request, HttpServletResponse response) {
		Page<NatatoriumInfo1> page = natatoriumInfoService1.findPage(new Page<NatatoriumInfo1>(request, response), natatoriumInfo1); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "form")
	public String form(NatatoriumInfo natatoriumInfo, Model model) {
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumInfoForm";
	}

	/**
	 * 保存游泳馆信息
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated NatatoriumInfo natatoriumInfo) {
		//校验排名是否有重复
		NatatoriumInfo natatoriumInfoWhere = new NatatoriumInfo();
		natatoriumInfoWhere.setRanking(natatoriumInfo.getRanking());		// 排名
		if(natatoriumInfo.getIsNewRecord()) {//新增
			if(natatoriumInfoService.findCount(natatoriumInfoWhere) >= 0) {
				return renderResult(Global.FALSE, text("排名已被占用。请更换！"));
			}
		} else {
			List<NatatoriumInfo> natatoriumInfoList = natatoriumInfoService.findList(natatoriumInfoWhere);
			if(CollectionUtils.isNotEmpty(natatoriumInfoList)) {
				for (NatatoriumInfo natatoriumInfo2 : natatoriumInfoList) {
					List<NatatoriumInfo> natatoriumInfoList1 = new ArrayList<NatatoriumInfo>();
					if(natatoriumInfo2.getRanking() != 1000) {//1000为新增的游泳馆默认排名
						natatoriumInfoList1.add(natatoriumInfo2);
					}
					if(CollectionUtils.isNotEmpty(natatoriumInfoList1)) {
						for (NatatoriumInfo natatoriumInfo1 : natatoriumInfoList1) {
							if(!natatoriumInfo1.getId().equals(natatoriumInfo.getId())) {
								return renderResult(Global.FALSE, text("排名已被占用。请更换！"));
							}
						}
					}					
				}
			}
		}
		natatoriumInfoService.save(natatoriumInfo);
		return renderResult(Global.TRUE, text("保存游泳馆信息成功！"));
	}
	
	/**
	 * 停用游泳馆信息
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(NatatoriumInfo natatoriumInfo) {
		natatoriumInfo.setStatus(NatatoriumInfo.STATUS_DISABLE);
		natatoriumInfoService.updateStatus(natatoriumInfo);
		return renderResult(Global.TRUE, text("停用游泳馆信息成功"));
	}
	
	/**
	 * 启用游泳馆信息
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(NatatoriumInfo natatoriumInfo) {
		natatoriumInfo.setStatus(NatatoriumInfo.STATUS_NORMAL);
		natatoriumInfoService.updateStatus(natatoriumInfo);
		return renderResult(Global.TRUE, text("启用游泳馆信息成功"));
	}
	
	/**
	 * 删除游泳馆信息
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(NatatoriumInfo natatoriumInfo) {
		natatoriumInfoService.delete(natatoriumInfo);
		return renderResult(Global.TRUE, text("删除游泳馆信息成功！"));
	}
	
	/**
	 * 跳转游泳馆信息页面
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:edit")
	@RequestMapping(value = "natatoriumInfoPage")
	public String natatoriumInfoPage(Model model) {
		NatatoriumInfo natatoriumInfo = new NatatoriumInfo();
		NatatoriumInfoToUser natatoriumInfoToUser = new NatatoriumInfoToUser();
		natatoriumInfoToUser.setUserCode(UserUtils.getUser().getUserCode());
		List<NatatoriumInfoToUser> natatoriumInfoToUserList = natatoriumInfoToUserService.findList(natatoriumInfoToUser);
		if (!CollectionUtils.isEmpty(natatoriumInfoToUserList)) {
			natatoriumInfo = natatoriumInfoService.get(natatoriumInfoToUserList.get(0).getNatatoriumInfoId());
		}
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumInfoPage";
	}
	
	/**
	 * 获取详情数据
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String natatoriumInfoId) {
		return renderResult(Global.TRUE, text("获取详情数据成功！"), natatoriumInfoService.get(natatoriumInfoId));
	}
	
	/**
	 * 查询附近场馆（按区域，评分选择6家）
	 */
	@RequestMapping(value = "findNearNatatoriumList")
	@ResponseBody
	public String findNearNatatoriumList(String natatoriumInfoId) {
		return renderResult(Global.TRUE, text("获取详情数据成功！"), natatoriumInfoService.findNearNatatoriumList(natatoriumInfoId));
	}
	
	/**
	 * 查询区域对应的场馆数量
	 */
	@RequestMapping(value = "findNatatoriumNum")
	@ResponseBody
	public Map<String, Long> findNatatoriumNum() {
		return natatoriumInfoService.findNatatoriumNum();
	}
	
//	/**
//	 * 获取场馆预约电话
//	 */
//	@RequestMapping(value = "getReservationPhone")
//	@ResponseBody
//	public String getReservationPhone(String natatoriumInfoId) {
//		return renderResult(Global.TRUE, text("获取场馆预约电话成功！"), natatoriumInfoService.getReservationPhone(natatoriumInfoId));
//	}
	
	//**************************************************************
	//************************ 访问量统计模块 ************************
	//**************************************************************
	
	/**
	 * 平台管理 - 游泳馆管理 - 访问量统计  查看游泳馆访问量统计页面
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "natatoriumTrafficPage")
	public String natatoriumTrafficPage(NatatoriumInfo natatoriumInfo, Model model) {
		model.addAttribute("clickStats",  new ClickStats());
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumTrafficPage";
	}
	
	/**
	 * 游泳馆管理 - 访问量统计 查看游泳馆访问量统计页面
	 */
	@RequiresPermissions("natatoriuminfo:natatoriumInfo:view")
	@RequestMapping(value = "natatoriumTrafficPage1")
	public String natatoriumTrafficPage1(HttpServletRequest request, Model model) {
		NatatoriumInfo natatoriumInfo = natatoriumInfoService.get(CookieUtils.getCookie(request, "natatoriumInfoId"));
		model.addAttribute("clickStats",  new ClickStats());
		model.addAttribute("natatoriumInfo", natatoriumInfo);
		return "swim/natatoriuminfo/natatoriumTrafficPage";
	}
	
	/**
	 * 查看平台访问量统计页面
	 */
	@RequiresPermissions("platformTraffic:platformTraffic:view")
	@RequestMapping(value = "platformTrafficPage")
	public String platformTrafficPage(Model model) {
		model.addAttribute("clickStats",  new ClickStats());
		return "swim/natatoriuminfo/platformTrafficPage";
	}
	
}